<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/inc/common.jsp"%>
<html>
<head>
<title>后台管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<div class="easyui-layout" fit="true">
			<div data-options="region:'north', title:'查询条件',split:true,collapsed:false,footer:'#searchfooter'" style="overflow: hidden;">
			<!-- 查询条件 -->
			<div style="padding:10px 5px 45px 20px;">
			<form id="searchForm" name="searchForm" style="margin-bottom: 0px;">
					<table class="search-table"  width="80%" cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td align="right" width="10%">角色名:</td>
							<td width="15%">
								<input class="easyui-textbox" type="text" id="name" name="name"></input>
							</td>
							<td align="right" width="10%">状态:</td>
							<td width="15%">
								<input class="easyui-combobox" name="state"  data-options="url:'<%=path%>/sys/dictionary/dicbykey.do?key=1001&type=1',method:'get',valueField:'dataCode',textField:'dataName',panelHeight:'auto',editable:false">
							</td>
						</tr>
					</table>
				</form>
			</div>
			<!-- 搜索按钮 -->
			<div id="searchfooter" style="text-align:right;padding: 0px 10px">
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="searchDate()"  data-options="iconCls:'icon-search',plain:true" >查询</a>
				<span class="search-btn-separator"></span>
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="cleanDate()" data-options="iconCls:'icon-cross',plain:true">重置</a>
			</div>
		</div>
		<!-- 数据列表 -->
		<div region="center" split="true" border="false" style="overflow: hidden;width: 99%;height: auto">
			<table id="dg"></table>
		</div>
		
		<!-- 弹出对话框 -->
		<div id="ew" class="easyui-window" style="width:700px;height:400px;padding:10px; background: #fafafa;" 
			data-options="modal:true,closed:true,iconCls:'icon-save',footer:'#dialogfooter'">
			<div class="easyui-layout" fit="true">
				<div region="center" border="false">
					<ul id="tree" class="ztree" style="width:auto; overflow:auto;"></ul>
				</div>
				<div id="dialogfooter" style="text-align:right;padding: 0px 10px">
					<span class="search-btn-separator"></span>
					<a href="javascript:void(0)" class="easyui-linkbutton" onclick="allotDate()"  data-options="iconCls:'icon-page_save',plain:true" >保存</a>
					<span class="search-btn-separator"></span>
					<a href="javascript:void(0)" class="easyui-linkbutton" onclick="closeWindow()" data-options="iconCls:'icon-cross',plain:true">关闭</a>
					<span class="search-btn-separator"></span>
				</div>
			</div>
		</div>
	</div>
</body>
</html>
<script type="text/javascript">
	$(function() {
		initDg();//初始化datagrid
		setDgBut(0,0);//初始化datagrid按钮
	});
	
	//回车查询
	$(document).keydown(function(e){
		if(e.keyCode == 13) {
			searchDate();
		}
	});
	
	//初始化数据列表
	function initDg(){
		$('#dg').datagrid({
			url:'${basePath}/sys/role/query.do',
			loadMsg:'数据装载中......',
		    title:'结果列表',//标题
		    width:'auto',//列表的宽度
			height:'auto',//列表的高度
			fit:true,//自动填充全屏
			nowrap: false,//数据是否显示在一行中
			fitColumns:true,//是否根据字体数收缩
		    pagination:true,//分页底部显示
		    pageList:[10,20,50],//设置分页下拉页数
		    rownumbers : true,//显示行号
			//singleSelect:true,//只能单选
			autoRowHeight:false,//自动行高
			idField : 'id',
		    columns:[[
				{field:'ck',checkbox:'true'},
				{field:'name',title:'角色名称',width:fixWidth(40),align:'center'},//宽度百分10
				{field:'state',title:'状态',width:fixWidth(20),align:'center',
					formatter:function(value,row,index){
		                return parent.formartDic(value,"1001");
		             }
				}
			]],
			toolbar:[
				{
					id : 'dg-add',
					text : '新增',
					iconCls : 'icon-add',
					handler : function() {
						editData();
					}
				},'-',{
					id : 'dg-edit',
					text : '修改',
					iconCls : 'icon-edit',
					handler : function() {
						var row = $('#dg').datagrid('getSelected');
						if (row) {
							editData(row.id);
						}else{
								$.messager.alert('消息', "请选择一条信息!",'info');	
							}
					}
				},'-',{
					id : 'dg-error',
					text : '禁用',
					iconCls : 'icon-error_delete',
					handler : function() {
						var row = $('#dg').datagrid('getSelected');
						if (row){
							if(row.state != '100102'){
								$.messager.confirm('系统提示', '您确定要禁用该条数据吗?', function(r) {
									if(r){
										updateStatus(row.id,'100102');
									}
								});
							}else{
								$.messager.alert('消息', "该条数据已经被禁用!",'info');	
							}
						}else{
								$.messager.alert('消息', "请选择一条信息!",'info');	
							}
					}
				},'-',{
					id : 'dg-ok',
					text : '启用',
					iconCls : 'icon-error_go',
					handler : function() {
						var row = $('#dg').datagrid('getSelected');
						if (row) {
							if(row.state != '100101'){
								$.messager.confirm('系统提示', '您确定要启用该条数据吗?', function(r) {
									if(r){
										updateStatus(row.id,'100101');
									}
								});
							}else{
								$.messager.alert('消息', "该条数据已经被启用!",'info');	
							}
						}else{
								$.messager.alert('消息', "请选择一条信息!",'info');	
							}
					}
				},'-',{
					id : 'dg-delete',
					text : '删除',
					iconCls : 'icon-delete',
					handler : function() {
						var row = $('#dg').datagrid('getSelections');
						if (row && row.length>0){
							$.messager.confirm('系统提示', '您确定要删除该条数据吗?', function(r) {
								if(r){
									deleteData(row);
								}
							});
						}else{
								$.messager.alert('消息', "请选择一条信息!",'info');	
						}
					}
				},'-',{
					id : 'dg-authorize',
					text : '分配菜单',
					iconCls : 'icon-coins_add',
					handler : function() {
						var row = $('#dg').datagrid('getSelected');
						if (row) {
							showWindow(row);
						}else{
							$.messager.alert('消息', "请选择一条信息!",'info');	
						}
					}
				}		
		   	],
            onSelect: function (index, row) {
            	setDgBut(1,1);//设置单选的按钮状态
            },
            onUnselect: function (index, row) {
            	setDgBut(2,1);//设置取消单选的按钮状态
            },
            onSelectAll: function (index, row) {
            	setDgBut(3,index.length);//设置全选的按钮状态
            },
            onUnselectAll: function (index, row) {
            	setDgBut(2,index.length);//设置取消全选的按钮状态
            }
		});
	}
	
	//查询
	function searchDate(){
		var params = $('#dg').datagrid('options').queryParams; //先取得 datagrid 的查询参数
        var fields =$('#searchForm').serializeArray(); //自动序列化表单元素为JSON对象  
        $.each( fields, function(i, field){
            params[field.name] = field.value; //设置查询参数  
        });
        $('#dg').datagrid('reload'); //设置好查询参数 reload 一下就可以了 
	}
	
	//编辑
	function editData(id){
		if(id != null && id != "undefined" && id != ""){
			window.location.href="${basePath}/sys/role/edit.do?id="+id;
		}else{
			window.location.href="${basePath}/sys/role/edit.do";
		}
	}
	
	//启用/禁用
	function updateStatus(id,state){
		$.ajax({
            url:'${basePath}/sys/role/updateState.do',  
            type:'post',
            data:"id="+id+"&state="+state, 
            dataType:"json",
            success:function(msg){
            	$.each(msg,function(i,n){
            		if(n.message == "success"){
            			$('#dg').datagrid('reload');
            		}else{
            			$.messager.alert('消息', "失败!请重新操作!");
            		}
               });
                 
           	}
         });
	}
	
	//删除
	function deleteData(row){
		var ids = "";
		for(var i=0;i<row.length;i++){
			ids = ids+","+row[i].id;
		}
		if(ids!="" && ids.length>0){
			ids = ids.substring(1, ids.length);
			$.ajax({
	            url:'${basePath}/sys/role/delete.do',  
	            type:'post',         
	            data:"ids="+ids, 
	            dataType:"json",
	            success:function(msg){
	            	$.each(msg,function(i,n){
	            		if(n.message == "success"){
	            			$('#dg').datagrid('reload');
	            		}else{
	            			$.messager.alert('消息', '失败!请重新操作!','warning');
	            		}
	               });
	                 
	           	}
	         });
		}
	}
	
	//清空
	function cleanDate(){
		$('#searchForm').form('clear');
	}
	
	//显示弹出窗体
	function showWindow(row){
		$("#ew").panel({title:'分配权限'});
		$('#ew').window('open');
		initWdg(row.id);
	}
	
	//关闭弹出窗体
	function closeWindow(){
		$('#ew').window('close');
	}
	
	//初始化弹出窗口的数据
	function initWdg(rid){
		var setting = {
			check:{
			    enable:true,//是否可勾选，只有设置成ture其他才生效
			    chkStyle: "checkbox",//勾选框类型
				radioType: "all"//radio 的分组范围
			},
			view: {
			    dblClickExpand: false,//双击节点时，是否自动展开父节点的标识
			    showLine: true,//是否显示节点之间的连线。
			    selectedMulti: true//是否允许同时选中多个节点。
			},
			data: {
			    simpleData: {
			    	enable:true,
			        idKey: "id",
			        pIdKey: "parentId",
			        rootPId: "0"
			    }
			}
		};
			
		$.ajax({
			type: "post",
			url: "${basePath}/sys/resources/queryResources.do?rid="+rid,
			dataType:"json",
			success:function(data){
				$.fn.zTree.init($("#tree"), setting, data);
			}
		});	
	}
	
	//分配菜单
	function allotDate(){
		var resid = "";//资源ID
		var roleRow = $('#dg').datagrid('getSelected');//角色行
		var treeObj = $.fn.zTree.getZTreeObj("tree");
		var nodes = treeObj.getCheckedNodes(true);
		for(var i=0;i<nodes.length;i++){
			if(nodes[i].id != 0){
				resid = resid+","+nodes[i].id;
			}
		}
		//判断是否有选中的菜单
		if(resid!=""){
			resid = resid.substring(1, resid.length);
			$.ajax({
	            url:'${basePath}/sys/role/allot.do',
	            type:'post',
	            data:"roleid="+roleRow.id+"&resid="+resid,
	            dataType:"json",
	            success:function(msg){
	            	$.each(msg,function(i,n){
	            		if(n.message == "success"){
	            			$.messager.alert('消息', "保存成功!");	
	            			closeWindow();
	            		}else{
	            			$.messager.alert('消息', "失败!请重新操作!");
	            		}
	               });
	           	}
	         });
		}else{
			$.messager.alert('消息', "请最少选择一个菜单!",'info');	
		}
	}
	
</script>